﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Truextend.Training.Model.Entities;
using Truextend.Training.Presenter;
using Truextend.Training.Presenter.ViewInterfaces;
using System.Collections.Generic;
using Microsoft.SharePoint;
using System.Web;
using Truextend.Training.Business.Dtos;

namespace Truextend.Training.CourseList
{
    public partial class CourseListUserControl : UserControl, IListCourseInstanceView
    {
        ListCourseInstancePresenter presenter;
        List<UserCourse> userCourses;
        List<CourseInstanceRegisterDto> courseInstances;
        SPUser user;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            SPWeb web = SPContext.Current.Web;
            user = web.CurrentUser;

            presenter = new ListCourseInstancePresenter(this);
            presenter.InitView(user.ID);

            DisableRegisterButton();

            //if (courseInstances.Count != 0)
            //{
            //    HLnkCalendar.NavigateUrl = SPContext.Current.Web.Url + "/_layouts/Truextend.Training/Pages/CoursesCalendarPage.aspx";
            //    HLnkCalendar.Visible = true;
            //}
        }

        public string Message
        {
            set { lblMessage.Text = value; }
        }

        public IList<CourseInstanceRegisterDto> CourseInstances
        {
            set
            {
                gvCourse.DataSource = value;
                gvCourse.DataBind();

                courseInstances = new List<CourseInstanceRegisterDto>(value);
            }
        }

        public IList<UserCourse> UserCourses
        {
            set { userCourses = new List<UserCourse>(value); }
        }

        protected void LnkRegister_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Register")
            {
                long courseInstanceId = Convert.ToInt64(e.CommandArgument);
                bool isRegistered = presenter.Register(user.ID, courseInstanceId);
                if (isRegistered)
                {
                    SPSite site = SPContext.Current.Site;
                    presenter.SendEMail(user.Email, courseInstanceId, site);
                    string url = HttpContext.Current.Request.Url.AbsoluteUri;
                    Response.Redirect(url);
                }
                else
                {
                    //Error
                }
            }
        }

        private void DisableRegisterButton()
        {
            if (userCourses.Count != 0)
            {
                foreach (GridViewRow row in gvCourse.Rows)
                {
                    int index = row.RowIndex;
                    CourseInstanceRegisterDto cv = courseInstances[index];
                    bool alreadyRegister = userCourses.Find(x => x.CourseInstance.CourseInstanceId == cv.CourseInstanceId) == null ? false : true;
                    bool isIntructor = (cv.InstructorId == user.ID);
                    if (alreadyRegister)
                    {
                        DisableLinkRegister(row, "You are already register", false);
                    }
                    if (isIntructor)
                    {
                        DisableLinkRegister(row, "You are instructor", true);
                    }
                }
            }
            else
            {
                foreach (GridViewRow row in gvCourse.Rows)
                {
                    int index = row.RowIndex;
                    CourseInstanceRegisterDto cv = courseInstances[index];
                    bool isIntructor = (cv.InstructorId == user.ID);
                    if (isIntructor)
                    {
                        DisableLinkRegister(row, "You are instructor", true);
                    }
                }
            }
        }

        private void DisableLinkRegister(GridViewRow row, string message, bool isInstructor)
        {
            LinkButton linkRegister = (LinkButton)row.FindControl("LnkRegister");
            Label labelRegister = (Label)row.FindControl("lblRegistered");
            labelRegister.Text = message;
            labelRegister.Visible = true;

            if (isInstructor)
            {
                linkRegister.Text = "Register";
                linkRegister.Enabled = false;
                linkRegister.OnClientClick = "";
            }
            else
            {
                linkRegister.Text = "Cancel";
                linkRegister.Enabled = true;
                linkRegister.OnClientClick = "return confirm('Are you sure you want to unregister to this course?');";
            }
        }
    }
}
